/*
 * Copyright [2009] [Marcin Rzeźnicki]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */
package mr.go.yaxc;

import java.lang.reflect.Constructor;

final class IntrospectiveActionInfo extends ActionInfo {

    private final Class<? extends Action> actionClass;
    private ActionInfo actionInfo;

    public IntrospectiveActionInfo(Class<? extends Action> actionClass) {
        this(actionClass,
             ActionMatch.ENDS_WITH,
             Actions.PRIORITY_NORMAL);
    }

    public IntrospectiveActionInfo(Class<? extends Action> actionClass,
                                   ActionMatch matchType, int priority) {
        this(actionClass,
             matchType,
             actionClass.getSimpleName(),
             priority);
    }

    public IntrospectiveActionInfo(Class<? extends Action> actionClass,
                                   ActionMatch matchType, String pattern,
                                   int priority) {
        super(matchType,
              pattern,
              priority);
        this.actionClass = actionClass;
        findActionInfo(actionClass,
                       matchType,
                       pattern,
                       priority);
    }

    public IntrospectiveActionInfo(Class<? extends Action> actionClass,
                                   int priority) {
        this(actionClass,
             ActionMatch.ENDS_WITH,
             priority);
    }

    private void findActionInfo(Class<? extends Action> actionClass,
                                ActionMatch matchType, String pattern,
                                int priority) {
        final String qualifiedActionName = actionClass.getName();
        final String proposedActionInfoName = qualifiedActionName + "Info";
        try {
            Class<? extends ActionInfo> actionInfoClass =
                    (Class<? extends ActionInfo>) Class.forName(
                    proposedActionInfoName);
            Constructor<ActionInfo> actionInfoConstructor = TypesHelper.
                    findConstructor(actionInfoClass,
                                    new Object[]{matchType,
                                                 pattern,
                                                 priority});
            actionInfo = actionInfoConstructor.newInstance(matchType,
                                                           pattern,
                                                           priority);
        } catch (Exception e) {
            // ok, it failed so we just won't do anything fancy with action info
        }
    }

    @Override
    public Class<? extends Action> getActionClass() {
        return actionClass;
    }

    @Override
    public Action getImplementation() {
        if (actionInfo != null) {
            return actionInfo.getImplementation();
        }
        try {
            return actionClass.newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException("Unable to make action instance",
                                       e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException("Unable to make action instance",
                                       e);
        }
    }
}
